package com.banciyuan.bcywebview.biz.zanlist;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.activity.BaseActivity;
import com.banciyuan.bcywebview.base.helper.ActionbarHelper;
import com.banciyuan.bcywebview.biz.novel.NovelFragment;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hy on 2015/4/28.
 */
public class ZanListActivity extends BaseActivity {

    private List<Fragment> mList = new ArrayList<>();
    private ViewPager mPager;
    private MyAdapter mAdapter;
    private ActionbarHelper mActionbarHelper;
    private View mActionBarView;
    private TabLayout mTabLayout;
    private String[] tabs;

    private ZanListFragment cosZanFragment;
    private ZanListFragment drawZanFragment;
    private NovelFragment novelZanFragment;
    private ZanListFragment dailyZanFragment;
    private GoodsZanListFragment goodsZanFragment;
    private RecommendListFragment recommendListFragment;
    private String uid;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zanlist_detail_viewpager_layout);
        initArgs();
        getFragment();
        initActionbar();
        initUi();
        initAction();
    }


    private void getFragment() {
        initRecommend();
        initCos();
        initDraw();
        initWrite();
        initDaily();
        initGoods();
        mList.add(recommendListFragment);
        mList.add(drawZanFragment);
        mList.add(novelZanFragment);
        mList.add(cosZanFragment);
        mList.add(dailyZanFragment);
        mList.add(goodsZanFragment);
    }

    private void initRecommend() {
        recommendListFragment = new RecommendListFragment();
        Bundle recommendBundle = new Bundle();
        recommendBundle.putString(gotoUtil.INTENT_VALUE_ONE, uid);
        recommendListFragment.setArguments(recommendBundle);
    }

    private void initGoods() {
        goodsZanFragment = new GoodsZanListFragment();
        Bundle goodsBundle = new Bundle();
        goodsBundle.putString("sub", "zhipin");
        goodsZanFragment.setArguments(goodsBundle);
    }

    private void initCos() {
        cosZanFragment = new ZanListFragment();
        Bundle cosBundle = new Bundle();
        cosBundle.putString("sub", "coser");
        cosZanFragment.setArguments(cosBundle);
    }

    private void initDraw() {
        drawZanFragment = new ZanListFragment();
        Bundle drawBundle = new Bundle();
        drawBundle.putString("sub", "drawer");
        drawZanFragment.setArguments(drawBundle);
    }

    private void initWrite() {
        novelZanFragment = new NovelFragment();
    }

    private void initDaily() {
        dailyZanFragment = new ZanListFragment();
        Bundle dailyBundle = new Bundle();
        dailyBundle.putString("sub", "daily");
        dailyZanFragment.setArguments(dailyBundle);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {

    }

    @Override
    protected void initArgs() {
        tabs = new String[]{getString(R.string.recommend_unit), getString(R.string.draw), getString(R.string.write), getString(R.string.cos), getString(R.string.daily), getString(R.string.goods)};
        uid = getIntent().getStringExtra(gotoUtil.INTENT_VALUE_ONE);
    }

    @Override
    protected void initActionbar() {
        mActionBarView = findViewById(R.id.base_action_bar);
        mActionbarHelper = new ActionbarHelper(this, mActionBarView);
        mActionbarHelper.setTitle(getString(R.string.parised));
    }

    @Override
    protected void initUi() {
        mTabLayout = (TabLayout) findViewById(R.id.tablayout);
        mAdapter = new MyAdapter(getSupportFragmentManager());
        mPager = (ViewPager) findViewById(R.id.pager);
        mPager.setOffscreenPageLimit(5);
        mPager.setAdapter(mAdapter);
        mTabLayout.setupWithViewPager(mPager);
        mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
    }


    class MyAdapter extends FragmentPagerAdapter {

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mList.get(position);
        }

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tabs[position];
        }
    }

}
